package com.jacobson.huobi.utils

import android.content.Context
import android.net.ConnectivityManager
import android.os.Build
import com.jacobson.huobi.HuobiApplication

object NetworkHelper {
    fun isNetworkAvailable(context: Context = HuobiApplication.getInstance()): Boolean {
        (context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager)?.run {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                this.allNetworks?.forEach { network ->
                    val networkInfo = this.getNetworkInfo(network)
                    if (networkInfo != null && networkInfo.isAvailable && networkInfo.isConnected) {
                        return true
                    }
                }
            } else {
                this.allNetworkInfo.forEach { networkInfo ->
                    if (networkInfo != null && networkInfo.isAvailable && networkInfo.isConnected) {
                        return true
                    }
                }
            }
        }
        return false
    }
}